package alvarodelrosal.chat.client.chataction.infraestructuraremota;

import alvarodelrosal.chat.client.modelo.Conversation;
import alvarodelrosal.chat.client.modelo.Message;
import java.util.ArrayList;
import java.util.List;

public class AllChats {

    private static List<Conversation> conversations = new ArrayList();
    private final Object lock = new Object();

    public static void addConversation(int id, String name) {
        if (!exists(id, name)) {
            conversations.add(new Conversation(id, name));
        }
    }

    private static boolean exists(int id, String name) {
        for (Conversation conversation : conversations) {
            if (conversation.getId() == id && conversation.getSubject().equals(name)) {
                return true;
            }
        }
        return false;
    }

    public static void addMessage(int id, int idUser, String message) {
        Conversation conversation = getConversation(id);
        conversation.addMessage(new Message(AllUsers.getUser(idUser), message));
    }

    private static Conversation getConversation(int id) {
        for (Conversation conversation : conversations) {
            if (conversation.getId() == id) {
                return conversation;
            }
        }
        throw new IllegalArgumentException("La conversacion no existe");
    }

    public static List<Conversation> getAllConversations() {
        return conversations;
    }
}
